package com.liholin.springboot.sample.config;

import com.liholin.springboot.sample.configdata.ResponseData;
import com.liholin.springboot.sample.exception.ErrorStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ValidExceptionHandler {
    @ExceptionHandler(BindException.class)
    public ResponseData validExceptionHandler(BindException exception) {
        String defaultMessage = exception.getAllErrors().get(0).getDefaultMessage();
        return ResponseData.Companion.error(defaultMessage == null ? "参数错误" : defaultMessage,  ErrorStatus.OPERATION_FAILED);
    }
}

